package j20251101;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

public class Demo4 {
    private final Map<Integer, ReentrantLock> segmentLocks = new HashMap<>();
    private final int segments = 16;

    public Demo4() {
        for (int i = 0; i < segments; i++) {
            segmentLocks.put(i, new ReentrantLock());
        }
    }

    public void doOperation(int key) {
        int segment = key % segments;
        ReentrantLock lock = segmentLocks.get(segment);
        lock.lock();
        try {
            // 操作对应分段的数据
        } finally {
            lock.unlock();
        }
    }
}
